home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / xnot12a.zip / SPAWN.C < prev    next >
C/C++ Source or Header  |  1993-06-04  |  8KB  |  336 lines

  1. #include "jam.h"
  2. /*
  3.  * Name:    Mg 2a
  4.  *        Spawn CLI for MSDOS (TurboC 1.5)
  5.  *
  6.  */
  7. #include "stdlib.h"
  8. #include "def.h"
  9. #include "keyname.h"
  10.  
  11. #if defined(WINDOWED) || defined (CURSES)
  12. # define CANSPAWN
  13.   static char command[NLINE + 1];
  14.   static char wincommand[NLINE + 1];
  15.  
  16. # ifdef MSW
  17.   static char makecommand[NLINE + 1] = {"nmake "};
  18. # else
  19.   static char makecommand[NLINE + 1] = {"make "};
  20. # endif
  21. #else
  22. # include <process.h>
  23.   extern char *getenv();
  24. #endif
  25.  
  26. static BOOL rn_(nextline, (char *filen, char *lnum));
  27.  
  28. #ifdef CANSPAWN
  29. spawncli(f, n, k)
  30. int f, n, k;
  31. {
  32.   return (winspawn((char *)0, FALSE));
  33. }
  34. spawncli2(f, n, k)
  35. int f, n, k;
  36. {
  37.   char buf[NLINE + 1];
  38.   int s;
  39.  
  40.   if (command)
  41.     epreload(command);
  42.  
  43. # ifdef MSW
  44.   s = ereply("!", buf, NFILEN);
  45. # else    
  46.   s = ereply("Shell: ", buf, NFILEN);
  47. # endif 
  48.  
  49.   if (s != TRUE)
  50.     return s;
  51.  
  52.   strcpy(command, buf);
  53.   WindowSleepCursor();
  54.   s = winspawn(buf, FALSE);
  55.   WindowNormalCursor();
  56.   return(s);
  57. }
  58.  
  59. # ifdef MSW
  60. int windowprog(int f, int n)
  61. {
  62.   char buf[NLINE + 1];
  63.   int s;
  64.  
  65.   if (wincommand)
  66.     epreload(wincommand);
  67.  
  68.   s = ereply("WinExec: ", buf, NFILEN);
  69.   if (s != TRUE)
  70.     return s;
  71.  
  72.   strcpy(wincommand, buf);
  73.   WindowSleepCursor();
  74.   s = winspawn(buf, TRUE);
  75.   WindowNormalCursor();
  76.   return(s);
  77. }
  78. # endif /* MSW */
  79.  
  80. # ifdef SomeUnix
  81. int peerprog(f, n)
  82. int f, n;
  83. {
  84.   char buf[NLINE + 1];
  85.   int s;
  86.  
  87.   if (wincommand)
  88.     epreload(wincommand);
  89.  
  90.   s = ereply("Exec: ", buf, NFILEN);
  91.   if (s != TRUE)
  92.     return s;
  93.  
  94.   strcpy(wincommand, buf);
  95.   WindowSleepCursor();
  96.   s = winspawn(buf, TRUE);
  97.   WindowNormalCursor();
  98.   return(s);
  99. }
  100. # endif
  101. #else /* CANSPAWN */
  102.  
  103. /*
  104.  * On MSDOS, we got no job control like system V, so always
  105.  * run a subshell. Bound to "C-C", and used
  106.  * as a subcommand by "C-Z". (daveb)
  107.  *
  108.  * Returns 0 if the shell executed OK, something else if
  109.  * we couldn't start shell or it exited badly.
  110.  */
  111. spawncli(f, n, k)
  112. int f, n, k;
  113. {
  114.     char *comspec;
  115.     int errp = FALSE;
  116.  
  117.     ttcolor(CTEXT);
  118.     ttnowindow();
  119.     ttmove(nrow-1, 0);
  120.     if (epresf != FALSE) {
  121.         tteeol();
  122.         epresf = FALSE;
  123.     }
  124.     ttclose();
  125.     sgarbf = TRUE;                /* Force repaint.    */
  126.     if ((comspec = getenv("COMSPEC")) == NULL)
  127.         errp = -1;
  128.     else
  129.         errp = spawnl(P_WAIT, comspec, "COMMAND.COM", (char *)NULL);
  130.  
  131.     ttopen();
  132.     if(errp == -1)
  133.       ewprintf("Failed to create process");
  134.  
  135.     return ( errp );
  136. }
  137. #endif /* WINDOWED */
  138.  
  139. /* This code has nothing specific to do with spawning a task -
  140. * it does however get compiled with OS specific stuff and
  141. * so I stuck the code in here cause this code knows (sorta) how to
  142. * parse a compile log. Note that on NT the stupid DOS window
  143. * directs stderr and stdout to different locations whereas
  144. * DOS windows under Window 3.1 direct both to the same place
  145. * thus this is only (currently) useful to Windows users, but
  146. * not NT or even UNIX folk. (jam)
  147. */
  148. sysmake(f, n)
  149. int f, n;
  150. {
  151. #ifdef CANSPAWN
  152.   char buf[NLINE + 1];
  153.   int s;
  154.  
  155.   if (makecommand)
  156.     epreload(makecommand);
  157.  
  158.   s = ereply("!", buf, NFILEN);
  159.  
  160.   if (s != TRUE)
  161.     return s;
  162.  
  163.   savebuffers(0, 0);                /* files up to date? */
  164.   update();
  165.   strcpy(makecommand, buf);            /* build make command */
  166.   WindowSleepCursor();
  167.   s = winspawn(buf, FALSE);        /* run it */
  168.   WindowNormalCursor();
  169.   return(s);
  170. #else
  171.   return(FALSE);
  172. #endif
  173. }
  174. /* parse spawned-task error file for compile errors
  175. */
  176. int makeerror(f, n)
  177. int f, n;
  178. {
  179. #ifdef CANSPAWN
  180.   BUFFER *bp;
  181.   EWINDOW *wp;
  182.  
  183.   if (!(bp = bfind(spawnfilename, FALSE)))
  184.     {
  185.       ewprintf("Can't find log file [%s].", spawnfilename);
  186.       return(FALSE);
  187.     }
  188.   
  189.   /* log file ?
  190.   */
  191.   if (bp)
  192.     {
  193.       char fname[NFILEN + 1];
  194.       char linenum[20];
  195.       char origdir[NFILEN + 1];
  196.  
  197.       if ((wp = popbuf(bp)) == NULL) 
  198.         return (FALSE);
  199.       curbp = bp;
  200.       curwp = wp;
  201.       thisflag |= CFNEWB;
  202.       if (nextline(fname, linenum))
  203.         {
  204.           /* Force dir back to original for filespecs
  205.           * w/o explicit path information
  206.           */
  207.           dirfrombp(origdir, bp);
  208.           gotodir(origdir);
  209.  
  210.           /* Legal error line might be found, but
  211.           * may not provide useful file-seeking info. 
  212.           * Check the return params for name and line
  213.           * number; always echo the error line
  214.           */
  215.           if (fname[0])
  216.             {
  217.               BUFFER *bp;
  218.  
  219.               AddString(fname);
  220.               AddKchar(CCHR('J'));
  221.               if (bp = bfind(fname, FALSE))
  222.                 poptobuffer(0, 1);        /* hope is same file */
  223.               else
  224.                 poptofilequiet(0, 1);        /* get file in cur dir */
  225.  
  226.               if (linenum[0])
  227.                 {
  228.                   AddString(linenum);
  229.                   AddKchar(CCHR('J'));
  230.                   gotoline(0, 1);
  231.                 }
  232.             }
  233.           ewprintf(&wp->w_dotp->l_text[0]);   /* log file window */
  234.         }
  235.       else
  236.         ewprintf("No more errors found.");
  237.     }
  238. #endif /* WINDOWED */
  239.   return (TRUE);
  240. }
  241. #ifdef CANSPAWN
  242. static BOOL nextline(fname, linenum)
  243. register char *fname, *linenum;
  244. {
  245. # if defined(MSW) || defined(SOL) || defined (HP)
  246.   for (;;) 
  247.     {
  248.       register char c;
  249. #ifdef HP
  250.       int cc_str = 0;
  251. #endif
  252.     
  253.       if (lforw(curwp->w_dotp) == curwp->w_bufp->b_linep)
  254.         return(FALSE);
  255.       curwp->w_dotp = lforw(curwp->w_dotp);
  256.       curwp->w_doto = 0;
  257.       c = lgetc(curwp->w_dotp, 0);
  258.  
  259.       /* See if line has filename and linenumber
  260.       */
  261.       if (!ISWHITE(c))
  262.         {
  263.           register int j, i;
  264.           BOOL dot = FALSE;
  265.           char lbuffer[NLINE];
  266.           int x = 0;
  267.  
  268. #ifdef HP
  269.           x += strlen("cc: ");
  270. #endif
  271.           j = i = 0; 
  272.           strcpy(lbuffer, &curwp->w_dotp->l_text[x]);
  273.           for (; lbuffer[i]; i++)
  274.             {
  275.               if (ISWHITE(lbuffer[i]))
  276.                 break;
  277.  
  278. #  ifdef MSW   
  279.     /* format is    file.c(xx)    xx is the line number */   
  280.               else if ((lbuffer[i] == '(') && dot)
  281.                 {
  282.                   i++;
  283. #  endif
  284.  
  285. #  if defined(SOL) || defined(HP)
  286.     /* SOL: format is    "file.c", line xx:  xx is line number  */
  287.     /* HP:  format is    cc: "file.c", line xx:  xx is line number  */
  288.               else if ((lbuffer[i] == ',') && dot)
  289.                 {
  290.                   char *str = ", line ";
  291.                   int n = (int)strlen(str);
  292.  
  293.                   if (strncmp(&lbuffer[i], str, n) != 0)
  294.                     return(FALSE);    /* don't know */
  295.                   else 
  296.                     i += n;
  297. #  endif
  298.                   /* some errors don't specifiy line numbers, ie LINK
  299.                   * errors s- return TRUE but zap return params
  300.                   */
  301.                   if (lbuffer[i] && !ISDIGIT(lbuffer[i]))
  302.                     {
  303.                       linenum[0] = '\0';
  304.                       fname[0] = '\0';
  305.                     }
  306.                   else
  307. #  ifdef MSW
  308.                     for (j = 0; lbuffer[i] && lbuffer[i] != ')'; )
  309. #  else
  310.                     for (j = 0; lbuffer[i] && lbuffer[i] != ':'; )
  311. #  endif
  312.                       {
  313.                         linenum[j++] = lbuffer[i++];
  314.                         linenum[j] = '\0';
  315.                       } 
  316.                   return(TRUE);
  317.                 }    
  318.               else
  319.                 {
  320. #  if defined (SOL) || defined (HP)      /* name is quoted, skip quotes */
  321.                   if (lbuffer[i] == '"')
  322.                     continue;
  323. #  endif
  324.                   fname[j++] = lbuffer[i];
  325.                   fname[j] = '\0';
  326.                   if (lbuffer[i] == '.')
  327.                     dot = TRUE;
  328.                 }    
  329.             }
  330.         }
  331.     }
  332. # endif /* MSW || SOL */
  333.   return (FALSE);
  334. }
  335. #endif /* CANSPAWN */
  336.